const figlet = promisify(require('figlet'))
const {clone} = require('./download')
const figlet = promisify(require('figlet'))
const clear = require('clear')
const chalk = require('chalk')
const open = require('open')
const path = require('path')

const spawn = async(...args) => {
    const {spawn} = require('child_process')
    return new Promise(resolve => {
        const proc = spawn(...args)
        proc.stdout.pipe(process.stdout)
        proc.stderr.pipe(process.stderr)
        proc.on('close',()=>{
            resolve()
        })
    })
    
}

const log = content => console.log(chalk.green(content))

module.exports = async name => {
    clear()
    const data = await figlet('aei start',{
        font:'Ghost'
    })
    log(data)

    // //项目模板
    // if(name)
    // {
    //     log('创建项目🚀🚀🚀 '+ name)
    //     await clone('su37josephxia/vue-template',name)
    // }


    //下载依赖 npm i
    //子进程
    log('等一下，很快了...')
    await spawn(process.platform === "win32" ? "npm.cmd" : "npm", ['install'], {
        //stdio: 'inherit',
        cwd:path.join(__dirname,`../${name}`),
        shell:true
    })    
    //同步 Promiseapi
    //输出流 子进程的流合并到主进程

    log(chalk.green(`
    👌安装完成：
    To get Start:
    ===========================
        cd ${name}
        npm run serve
    ===========================
    `))
    
    // await spawn(process.platform === "win32" ? "npm.cmd" : "npm", ['run','serve'], {
    //     //stdio: 'inherit',
    //     cwd:path.join(__dirname,`../${name}`),
    //     shell:true
    // })  
    // open('http://localhost:8080')
}